package com.qk.management.exception;

import com.qk.common.Result;
import com.qk.common.exception.CommonException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.Arrays;

/**
 * @author 16778
 * @date 2025/10/7 10:38
 * @description
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler
    public Result ex(SQLIntegrityConstraintViolationException e){
        String s = e.getMessage();
        String[] s1 = s.split(" ");
        System.out.println(Arrays.toString(s1));
        String error = s1[2].substring(1,s1[2].length()-1)+"已存在";
        return Result.error(error);
    }

    @ExceptionHandler
    public Result ex(CommonException e){
        log.info("异常信息：{}",e.getMessage());
        return Result.error(e.getMessage());
    }
}
